# Contributor: Newbyte <newbie13xd@gmail.com>
# Contributor: Nulo <alpine@nulo.in>
# Maintainer: Nulo <alpine@nulo.in>
pkgname=tg_owt
pkgver=0_git20230501
pkgrel=2
_commit="dcb5069ff76bd293e86928804208737e6cee2ccc"
_libsrtp_commit="a566a9cfcd619e8327784aa7cff4a1276dc1e895"
_libyuv_commit="00950840d1c9bcbb3eb6ebc5aac5793e71166c8b"
_crc32c_commit="21fc8ef30415a635e7351ffa0e5d5367943d4a94"
pkgdesc="Telegram Desktop's fork of Google's WebRTC"
url="https://github.com/desktop-app/tg_owt"

# matched to telegram-desktop
arch="all !x86 !ppc64le !s390x !armhf !riscv64"

license="BSD-3-Clause"

# Sorted according to
# https://github.com/telegramdesktop/tdesktop/wiki/The-Packaged-Building-Mode
# https://github.com/desktop-app/tg_owt/pull/55#discussion_r599718405
# openh264 bundled because packaged is in testing
# Not specified in the wiki page (see
# https://github.com/desktop-app/tg_owt/pull/55):
# pffft bundled because there's no stable ABI and patched
# rnnoise bundled because "all remaining files are custom"
# libsrtp bundled because tg_owt uses private APIs
# libyuv bundled because there's no stable ABI and has many breaking updates
# crc32c bundled because it isn't packaged and it is unclear if it will work
# packaged
depends_dev="
	abseil-cpp-dev
	libdrm-dev
	libepoxy-dev
	ffmpeg-dev
	mesa-dev
	glib-dev
	jpeg-dev
	openssl-dev>3
	opus-dev
	pipewire-dev
	libvpx-dev
	libx11-dev
	libxcomposite-dev
	libxdamage-dev
	libxext-dev
	libxfixes-dev
	libxrender-dev
	libxrandr-dev
	libxtst-dev
	"
makedepends="
	$depends_dev
	cmake
	yasm
	samurai
	"

subpackages="$pkgname-dev"
source="
	$pkgname-$_commit.tar.gz::https://github.com/desktop-app/tg_owt/archive/$_commit.tar.gz
	libyuv-$_libyuv_commit.tar.gz::https://github.com/klemensn/libyuv/archive/$_libyuv_commit.tar.gz
	crc32c-$_crc32c_commit.tar.gz::https://github.com/google/crc32c/archive/$_crc32c_commit.tar.gz
	libsrtp-$_libsrtp_commit.tar.gz::https://github.com/cisco/libsrtp/archive/$_libsrtp_commit.tar.gz
	abseil.patch
	cstdint.patch
	gcc12.patch
	gcc13.patch
	"
builddir="$srcdir/$pkgname-$_commit"

prepare() {
	default_prepare
	mv ../libyuv-$_libyuv_commit/* src/third_party/libyuv
	mv ../crc32c-$_crc32c_commit/* src/third_party/crc32c/src
	mv ../libsrtp-$_libsrtp_commit/* src/third_party/libsrtp/
}

build() {
	case "$CARCH" in
	arm*)
		# our armv7 baseline has no neon
		export CXXFLAGS="$CXXFLAGS -DLIBYUV_DISABLE_NEON"
		;;
	esac
	# dynamic version has broken linking
	cmake -B build -G Ninja \
		-DTG_OWT_PACKAGED_BUILD=True \
		-DBUILD_SHARED_LIBS=OFF \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_BUILD_TYPE=MinSizeRel

	cmake --build build
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

sha512sums="
32870f1901b6dcd164263e7ecfecc1c8529f9507e77d2a5e3b31e7e95d4b1387988ef02a67040c1c7e50777cba067a10026ed9a505bd4bb6549719a4da5c8a82  tg_owt-dcb5069ff76bd293e86928804208737e6cee2ccc.tar.gz
9f455c4e21cd168daa66f41f486a2b73d298dd1ce74e30856a2623eb9b77058318468177cee6b98b882baec1d2585336817fe8f765018fdf0430870c231d4ec2  libyuv-00950840d1c9bcbb3eb6ebc5aac5793e71166c8b.tar.gz
80dc12463c95d87a25eac49249ea303d6e4734bcf9a849dbcc564c70df1b1b5f70e7f7e4db96f4c1c66bb3d4eeca87aeef9f2312a8162ade885f09b049fd7417  crc32c-21fc8ef30415a635e7351ffa0e5d5367943d4a94.tar.gz
930e665434e80e8a26d81b785563c915619b546a9a0af0455a14278816997074add852fae107027f4899415cb0ab47ffbf9492bea76b94b10f558b52098bbf92  libsrtp-a566a9cfcd619e8327784aa7cff4a1276dc1e895.tar.gz
b1d609500ad24bb015b3ab7ad3775f2667d674155abb12dd35d8e10b83c5e91c69fd8e415fd6543a45608b3dfd7fa72eb4ce9ec960e5742fb04cafaa20f6f206  abseil.patch
b530508390370ca1ed0728e4429920a7d3132293f3d8a2a3f3220efd2c00ceb7a40ee58184f595b1101389133570257900e4a44e7611d4064f7220b67c33b93b  cstdint.patch
2eb235583c952b07f19e455f79a0d464b77f7fce99b5434bff54f7cea2770c117261409c70d7963c78c55822b45c6da467e966df1bcadda70048673b73ed3a95  gcc12.patch
329f60f69f61d783ac3474cd8812d3a85a4298df75e4aead3a960a4714382fd5425ef867f7b8c941e1b6a8231d968b4384f36bf9770cb434d0cbffeacdf3a5a9  gcc13.patch
"
